package com.example.recordingpen_datacollection.AIBDText;

import com.alibaba.fastjson.JSONObject;
import com.example.recordingpen_datacollection.Utils.AIUtil.AIChangeUtil;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.Utils.Token.UniqueTokenUtil;
import com.example.recordingpen_datacollection.Utils.http.OkHttpUtils;
import com.example.recordingpen_datacollection.entity.Vo.LimitDosageVo;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class test {
    @Test
    public void 上下文(){
        List<String> list = new ArrayList<>(10);

        for (int i = 0; i < 15; i++) {
            if(list.size()>10){
                list.remove(0);
            }
         list.add(i+"");
        }

        for (String s : list) {
            System.out.print(s+", ");
        }
    }
    @Test
    public void 上下文2(){
        LimitedSizeList<String> list = new LimitedSizeList<>(10);
        for (int i = 0; i < 15; i++) {
            list.add("Item " + i);
            System.out.println(list); // 打印当前列表的内容
        }
    }

    @Test
    public void tiken长度(){
        String jsonString  = "{\"content\":\"1+1\",\"role\":\"user\"}";
        System.out.println( JSONObject.parseObject(jsonString));
        boolean a = false;
//        for(int i =0; i<5;i++){a
//            String token = UniqueTokenUtil.generateToken();
//            System.out.println(token +" : "+token.length() );
//        }
    }

    @Test
    public void tois(){
        LimitDosageVo limitDosageVo = new LimitDosageVo();
        limitDosageVo.setLimit(-1);
//        limitDosageVo.setEndTime("1");
        System.out.println(limitDosageVo.isAtLeastOneMemberNotNull());

    }


    @Test
    public void testHttp() throws InterruptedException {
        System.out.println("准备开始-------------------------------》》》》");
//        Thread.sleep(1000*60*3);
        System.out.println("开始运行---------------------------------》》》》》》》》》");
        ExecutorService pool = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 1; i++) {
            //i%2 == 0
            if(false){
                pool.execute(new PrintTask("http://localhost:10487/AI/VIPtxtSumUp","219de75a-781c-4c9d-b43f-e8a60e777aba"));
            }
            else {
                pool.execute(new PrintTask("http://47.119.165.185:10487/AI/VIPtxtSumUp","252e8d50-24a9-4fe2-8697-538043aa454c"));
            }
//                            Thread.sleep(10);

//            if(i%20 == 0){
//                Thread.sleep(1000*30);
//            }
        }

        while (true);
    }

}
class PrintTask implements Runnable{
    public String url = null;
    public String token = null;
    public static int  i = 0;
    public static synchronized void increment() {
        i++;
    }

    public static synchronized int getValue() {
        return i;
    }

    public PrintTask(String url, String token) {
        this.url = url;
        this.token = token;
    }

    @Override
    public void run() {
//        String url = "http://localhost:10487/AI/VIPtxtSumUp";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content","1+1");
        jsonObject.put("serverType",5);
        jsonObject.put("textIs",false);
        jsonObject.put("textName","string"+getValue());

        String jsonBody = "{\"content\":\"1+1\",\"serverType\":5,\"textIs\":false,\"textName\":\"string"+getValue()+"\"}";
        System.out.println(jsonObject.toJSONString());
        System.out.println(jsonBody);

        Map<String, String> headers = new HashMap<>();
        System.out.println(token);
        System.out.println(url);
        headers.put("token",token);
        String s = doPost3(url, jsonBody,token);
        increment();
        System.out.println(s + " <-> " +getValue() +" -> " + url);
        System.out.println(url+"结束当前请求时间-------------》"+ TimeJudgeUtils.getNowDate());
    }

    private static HttpLoggingInterceptor logger = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
    public static String doPost3(String url, String json, String token){
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS) // 设置连接超时时间为30秒
                .readTimeout(60, TimeUnit.SECONDS) // 设置读取超时时间为60秒
                .writeTimeout(60, TimeUnit.SECONDS) // 设置写入超时时间为60秒
                .addInterceptor(logger)
                .build();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOm51bGwsInNjb3BlIjpbImFsbCJdLCJhcHBJZCI6Inp5N2FUcnFPdTAiLCJjdXN0b21lcklkIjoxNzgyNjY1NzI5OTA1MTM1NjE2LCJleHAiOjE3MTkzNjAxNzEsImp0aSI6ImVkMTUwNjQyLTY1MmEtNDBiMC1iNTI5LTg2MGVjYzBmN2Q2MSIsImNsaWVudF9pZCI6Inp5N2FUcnFPdTAifQ.dk0Tqb_w3oYHn9w2aak5mwokv_E_7VagPoC6BbcvS-KyFC2ddp3Zfuf5dF9QFbFEVo90ziI0CvOCp2fc83c37s84LtmRyBf3bCogXKIQm47WYlqsOwu0fkwsu7Zf188hjPsxlsahqpU2yqJS9HC3Jc4Hu29j2wRXT89J0UqjfxOfpRxoWyu5GBYz8xqRACll2DOsrbzwkChT0XKDENsIaUIHHufAV-mU8zpUUQ47VgfdOxLJ7wTne2NCQe0B2ORRpaXUChAVpLOFtAgYbSP4FTy0SAhvHBT6151FibgdM3WSFkvC9b2TJXyBDGuPmUkdHNAK8K5i-I96ilNyIGXvpA")
                .addHeader("content-type", "application/json")
                .addHeader("token", token)
                .build();

        try {
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()){
                System.out.println(response.body().string());
                throw new IOException("远程调用接口失败：" + response);
            }

            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}